import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  CURRENT_HMOS_API
} from '../../../../../models/constants';
import { ModuleTemplateOption, ModuleTemplateOptionBuilder } from '../../../../../models/model-options';
import { ModuleConfigInitFun } from '../../../../../models/module-models';
import { copyTmp, DirTemplateCreator, dirTmp, renderTmp } from '../../../../../models/template-models';
import { moduleBuilder } from '../../../../../module-builder';

const featureOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.STAGE)
  .apiVersion(CURRENT_HMOS_API)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.FeatureEmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const featureTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/app/feature/api16/src/main/ets/featureability'))
        .add(copyTmp('/module/stage/app/feature/api16/src/main/ets/pages'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/app/feature/api16/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/stage/app/feature/api16/src/main/resources/base/element/string.json'))
          )
          .add(copyTmp('/module/stage/app/feature/api16/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/app/feature/api16/src/main/resources/base/profile'))
        )
        .add(copyTmp('/module/stage/app/feature/api16/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/module/stage/app/feature/api16/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/stage/app/feature/api16/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/stage/app/feature/api16/src/ohosTest/ets'))
      .add(renderTmp('/module/stage/app/feature/api16/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/module/stage/app/feature/api16/src/test'))
  )
  .add(renderTmp('/module/stage/app/feature/api16/build-profile.json5'))
  .add(copyTmp('/module/stage/app/feature/hvigorfile.ts', 'hvigorfile.ts'))
  .add(copyTmp('/module/stage/app/feature/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/app/feature/oh-package.json5'));


const initFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('src').node('ohosTest').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType, runtimeOS: option.runtimeOS };
};

moduleBuilder.add(featureOption, featureTemplate, initFun);
